﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace LeoMVC.DateBaseSchemaCreator
{
    class Program
    {
        const string Connstring = "server=.\\SQLExpress;" +
            "database=LeoMVC;" +
            "Integrated Security=SSPI;"; 
        static void Main(string[] args)
        {
            Console.WriteLine("Press Y to create the Database Schema..");
            var input = Console.ReadLine();
            if(input=="Y")
            {
                CreateConfiguration();
            }
            Console.WriteLine("Database Schema is Created Successfully!");
            Console.ReadLine();
        }

        static void CreateConfiguration()
        {
            Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008.ConnectionString(Connstring))
                //如果使用fluent NHibernate提供的映射类来mapping则使用FluentMappings
                //.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
                //.Mappings(m => m.FluentMappings.AddFromAssembly(System.Reflection.Assembly.Load("LeoMVC.Mapping")))
                //如果使用hbm.xml文件来mapping，则使用HbmMappings
                .Mappings(m => m.HbmMappings.AddFromAssembly(System.Reflection.Assembly.Load("LeoMVC.Mapping")))
                .ExposeConfiguration(CreateSchema)
                .BuildConfiguration();
        }

        static void CreateSchema(Configuration cfg)
        {
            var schemaExport = new SchemaExport(cfg);
            //第一个true表示是否在控制台打印出ddl语句，第二个true表示是否执行这些ddl语句。
            schemaExport.Drop(true,true);
            schemaExport.Create(true,true);
        }

    }
}
